var byte_array = new Array(7);
var component_value = [];
var conversion_tbl = [];
function init_conversion_tbl() {
    for (i = 0; i < comp_sum; i++) 
	conversion_tbl[comp_collection[i]] = i;
}
function init_array(arr, v) {
    for (i = 0; i < comp_sum; i++) 
	arr[comp_collection[i]] = v;
}
function showResult() {
    show_result = document.getElementById("InputResult");
    var check_result = document.getElementsByTagName("input");
    var checked_comp_list = [];
    var unchecked_items = '';
    var checked_comp_desc = '';
    var item_temp = '';
    var hex_temp = '';
    var hex_result = '';
    init_array(component_value, 0);
    init_array(checked_comp_list, 0);
    //show_result.innerHTML = "";
    show_result.value = "AA ";
    for(i = 0; i < check_result.length; i++) {
	if(check_result[i].checked) {
	    var result = Number(check_result[i].value).toString(2);
	    var result_len = Number(check_result[i].value).toString(2).length;
	    component_value[check_result[i].name] = Number(check_result[i].value);
	    checked_comp_list[check_result[i].name] = 1;
	}
    }
    for(i in checked_comp_list) {
	if(component2choose[i] == 1) {
	    if (checked_comp_list[i] != 1)
		unchecked_items += i +  '\n';
	    else 
		checked_comp_desc += '[' + i + '] ' + ':' + spec_desc_collection[conversion_tbl[i]][component_value[i]] + '\n';
	}
    }
    if(unchecked_items != '') {
	alert(unchecked_items + "has not checked.");
    } else {
	compute_Bid();
	alert(checked_comp_desc);
	for(j = 0; j < 7; j++) {
	    hex_temp = parseInt(byte_array[j],2).toString(16);
	    if(hex_temp.length == 1) { hex_result = "0" + hex_temp; }
	    else { hex_result = hex_temp; }
	    //show_result.innerHTML += byte_array[j] + " " + hex_result +  " ";
    	    show_result.value += hex_result +  " ";
	    //show_result.innerHTML += "<br />";
	}
    }
}
function do_disassemble_Bid()
{
    check_result = document.getElementsByTagName("input");
    for(i = 0; i < check_result.length; i++) {
	if (check_result[i].id == "BoardId")
	    bid = check_result[i].value;
    }
    //alert("Boarid is "+bid+", length="+bid.length+" magic #="+bid.substring(0,2));
    if ((bid.length != 16) || ((bid.substring(0,2) != "aa") && (bid.substring(0,2) != "AA")))  {
	alert ("Incorrect BoardId: "+bid);
	return;
    }
    var Bid = [];
    Bid[0] = bid.substring(2,4);
    Bid[1] = bid.substring(4,6);
    Bid[2] = bid.substring(6,8);
    Bid[3] = bid.substring(8,10);
    Bid[4] = bid.substring(10,12);
    Bid[5] = bid.substring(12,14);
    Bid[6] = bid.substring(14,16);
    //alert("Bid="+Bid);
    disassemble(Bid);
    var Bid_spec = "";
    for(i in component_value) {
	Bid_spec += '[' + i + '] ' + ':' + spec_desc_collection[conversion_tbl[i]][component_value[i]] + '\n';
    }
    alert(Bid_spec);
}
// dis-assemble the Bid to component vector
function disassemble(Bid)
{
    var checked_comp_list = [];
    var result = [];
    //alert("Inside disassemble");
    init_array(component_value, 0);
    //alert("a/f init component_value array");
    init_array(checked_comp_list, 1);
    //alert("Inside disassemble 1");
    // convert the BoardId into binary form first
    for (i = 0; i < 7; i++) {
	result[i] = parseInt(Bid[i], 16).toString(2);
	var j = result[i].length;
	//alert(result[i]+"len="+j);
	while (j < 8) {
	    result[i] = "0" + result[i];
	    j++;
	}
	//alert("a/f: "+result[i]);
    }
    //alert("Inside disassemble 2:"+result);
    component_value["LCD"] = parseInt(result[0].substring(0,5), 2);
    component_value["TP"] = parseInt(result[0].substring(5,8), 2);
    component_value["DDR"] = parseInt(result[1].substring(0,4), 2);
    component_value["EMMC"] = parseInt(result[1].substring(4,8), 2);
    component_value["FRONT_CAMERA"] = parseInt(result[2].substring(0,4), 2);
    component_value["BACK_CAMERA"] = parseInt(result[2].substring(4,8), 2);
    component_value["GPS"] = parseInt(result[3].substring(0,3), 2);
    component_value["VIBRATOR"] = parseInt(result[3].substring(3,4), 2);
    component_value["LIGHTSENSOR"] = parseInt(result[3].substring(4,7), 2);
    component_value["ACCELERATOR"] = parseInt((result[3].substring(7,8)+result[4].substring(0,2)), 2);
    component_value["GYRO"] = parseInt(result[4].substring(2,5), 2);
    component_value["MAGNETICSENSOR"] = parseInt(result[4].substring(5,8), 2);
    component_value["AUDIOCODEC"] = parseInt(result[5].substring(0,3), 2);
    component_value["WWLAN3G"] = parseInt(result[5].substring(3,6), 2);
    component_value["HDMI"] = parseInt(result[5].substring(6,8), 2);
    component_value["BLUETOOTH"] = parseInt(result[6].substring(0,2), 2);
    component_value["WIFI"] = parseInt(result[6].substring(2,6), 2);
    component_value["MICROPHONE"] = parseInt(result[6].substring(6,8), 2);
    //for (i in component_value) {
    //alert("component_value["+i+"]="+component_value[i]);
    //}
}
// Compute Board from the component vector
function compute_Bid() {
    for(i = 0; i < 7; i++) { byte_array[i] = new String; }
    //case "LCD":
    var result;
    var result_len;


    result = Number(component_value["LCD"]).toString(2);
    result_len = result.length;
    //alert("LCD:" + result);
    if(result_len < 5) 
	for(j = 0; j < (5 - result_len); j++) 
	    byte_array[0] += "0";
    byte_array[0] += result;
    //case "TP":
    result = Number(component_value["TP"]).toString(2);
    result_len = result.length;
    //alert("TP:" + result);
    if(result_len < 3) 
	for(j = 0; j < (3 - result_len); j++) 
	    byte_array[0] += "0";
    byte_array[0] += result;
    //case "DDR":
    result = Number(component_value["DDR"]).toString(2);
    result_len = result.length;
    //alert("DDR:" + result);
    if(result_len < 4) 
	for(j = 0; j < (4 - result_len); j++) 
	    byte_array[1] += "0";
    byte_array[1] += result;
    //case "EMMC":
    result = Number(component_value["EMMC"]).toString(2);
    result_len = result.length;
    //alert("EMMC:" + result);
    if(result_len < 4)
	for(j = 0; j < (4 - result_len); j++) 
	    byte_array[1] += "0";
    byte_array[1] += result;
    //case "FRONT_CAMERA":
    result = Number(component_value["FRONT_CAMERA"]).toString(2);
    result_len = result.length;
    //alert("FRONT_CAMERA:" + result);
    if(result_len < 4) 
	for(j = 0; j < (4 - result_len); j++)
	    byte_array[2] += "0";
    byte_array[2] += result;
    //case "BACK_CAMERA":
    result = Number(component_value["BACK_CAMERA"]).toString(2);
    result_len = result.length;
    //alert("BACK_CAMERA:" + result);
    if(result_len < 4) 
	for(j = 0; j < (4 - result_len); j++) 
	    byte_array[2] += "0";
    byte_array[2] += result;
    //case "GPS":
    result = Number(component_value["GPS"]).toString(2);
    result_len = result.length;
    //alert("GPS:" + result);
    if(result_len < 3) 
	for(j = 0; j < (3 - result_len); j++) 
	    byte_array[3] += "0";
    byte_array[3] += result;
    //case "VIBRATOR":
    result = Number(component_value["VIBRATOR"]).toString(2);
    result_len = result.length;
    //alert("VIBRATOR:" + result);
    byte_array[3] += result;
    //case "LIGHTSENSOR":
    result = Number(component_value["LIGHTSENSOR"]).toString(2);
    result_len = result.length;
    //alert("LIGHTSENSOR:" + result);
    if(result_len < 3) 
	for(j = 0; j < (3 - result_len); j++) 
	    byte_array[3] += "0";
    byte_array[3] += result;
    //case "ACCELERATOR":
    result = Number(component_value["ACCELERATOR"]).toString(2);
    result_len = result.length;
    //alert("ACCELERATOR:" + result);
    if(result_len < 3) { 
	for(j = 0; j < (3 - result_len); j++) {
	    if(j == 0) { byte_array[3] += "0"; }
	    else { byte_array[4] += "0"; }
	}
	byte_array[4] += result;
    } else {
	for(j = 0; j < 3; j++) {
	    if(j == 0) { byte_array[3] += result[0]; }
	    else { byte_array[4] += result[j]; }
	}
    }
    //case "GYRO":
    result = Number(component_value["GYRO"]).toString(2);
    result_len = result.length;
    //alert("GYRO:" + result);
    if(result_len < 3) 
	for(j = 0; j < (3 - result_len); j++) 
	    byte_array[4] += "0";
    byte_array[4] += result;
    //case "MAGNETICSENSOR":
    result = Number(component_value["MAGNETICSENSOR"]).toString(2);
    result_len = result.length;
    //alert("MAGNETICSENSOR" + result);
    if(result_len < 3) 
	for(j = 0; j < (3 - result_len); j++) 
	    byte_array[4] += "0";
    byte_array[4] += result;
    //case "AUDIOCODEC":
    result = Number(component_value["AUDIOCODEC"]).toString(2);
    result_len = result.length;
    //alert("AUDIOCODEC" + result);
    if(result_len < 3) 
	for(j = 0; j < (3 - result_len); j++) 
	    byte_array[5] += "0";
    byte_array[5] += result;
    //case "WWLAN3G":
    result = Number(component_value["WWLAN3G"]).toString(2);
    result_len = result.length;
    //alert("WWLAN3G" + result);
    if(result_len < 3) 
	for(j = 0; j < (3 - result_len); j++) 
	    byte_array[5] += "0";
    byte_array[5] += result;
    //case "HDMI":
    result = Number(component_value["HDMI"]).toString(2);
    result_len = result.length;
    //alert("HDMI" + result);
    if(result_len < 2) 
	for(j = 0; j < (2 - result_len); j++) 
	    byte_array[5] += "0";
    byte_array[5] += result;
    //case "BLUETOOTH":
    result = Number(component_value["BLUETOOTH"]).toString(2);
    result_len = result.length;
    //alert("BLUETOOTH" + result);
    if(result_len < 2) 
	for(j = 0; j < (2 - result_len); j++) 
	    byte_array[6] += "0";
    byte_array[6] += result;
    //case "WIFI":
    result = Number(component_value["WIFI"]).toString(2);
    result_len = result.length;
    //alert("WIFI" + result);
    if(result_len < 4) 
	for(j = 0; j < (4 - result_len); j++) 
	    byte_array[6] += "0";
    byte_array[6] += result;
    //case "MICROPHONE":
    result = Number(component_value["MICROPHONE"]).toString(2);
    result_len = result.length;
    //alert("MICROPHONE" + result);
    if(result_len < 2) 
	for(j = 0; j < (2 - result_len); j++) 
	    byte_array[6] += "0";
    byte_array[6] += result;
}